const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceOrder = require("../../../service/service_order.js");
const ServiceUserAmount = require("../../../service/service_userAmount.js");
const ServicePay = require("../../../service/service_pay.js");

module.exports = {
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    /**
     * 订单数据格式
        {
          id: 1,
          name: "法国进口红酒拉斐天使庄园干红葡萄酒红酒整箱礼盒装750ml*6",
          price: 100,
          imgUrl:''
        }
     */
    order: { amount: 0 },

    /**
     * 用户账号数据格式
        {accountName: "123", accountNumber: "45", account: "677"}
     */
    userAccount: null,
    //
    ticketPrice: 0,
    ticketId: null,
    score: 0
  },

  methods: {

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      const _this = this; ticketPrice
      const orderId = options.orderId;
      const ticketPrice = options.ticketPrice;
      const ticketId = options.ticketId;
      const score = options.score;
      // 加载订单数据
      ServiceOrder.findById(orderId, function (data) {
        _this.setData({
          order: data,
          score: isNaN(parseFloat(score)) ? 0 : parseFloat(score),
          ticketPrice: isNaN(parseFloat(ticketPrice)) ? 0 : parseFloat(ticketPrice),
          ticketId: isNaN(parseInt(ticketId)) ? null : parseInt(ticketId)
        })
      })
      // 加载用户账号数据
      ServiceUserAmount.findById(orderId, function (data) {
        _this.setData({
          userAccount: data
        })
      });
    },

    /**
     * 提交
     */
    onSubmitOrder: function (e) {
      const _this = this;
      const orderId = _this.data.order.id;
      //
      ServicePay.payUnderLine(
        orderId, 
        _this.data.ticketId,
        _this.data.score,
        function () {
          wx.navigateTo({ url: '../../../model_user/pages/order/order?state=2' })
        }
      );
    }
  }

}